#!/bin/bash

# 优化端口耗尽问题的配置脚本
cat << EOF | sudo tee -a /etc/sysctl.conf >/dev/null
# 端口范围优化 (默认范围 32768-60999)
net.ipv4.ip_local_port_range = 1024 65535

# 启用TIME-WAIT快速回收 (内核4.1+)
net.ipv4.tcp_tw_reuse = 1

# 增大连接跟踪表大小 (按需调整)
net.netfilter.nf_conntrack_max = 131072

# 增大TCP缓冲区大小
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216

# 减少FIN超时时间 (加速连接回收)
net.ipv4.tcp_fin_timeout = 30

# 增大最大半连接队列 (防SYN洪水)
net.ipv4.tcp_max_syn_backlog = 16384
EOF

# 立即应用配置
sudo sysctl -p

# 验证端口范围配置
echo -e "\n当前可用端口范围:"
cat /proc/sys/net/ipv4/ip_local_port_range